package com.mf.controller;


import com.mf.groups.GroupsAddInterface;
import com.mf.groups.GroupsUpdateInterface;
import com.mf.result.Result;
import com.mf.service.ServiceSmashDetail;
import com.mf.utils.UtilConverter;
import com.mf.entity.req.publicReq.ReqPublicIds;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.entity.dto.DtoSmash;
import com.mf.entity.req.ReqSmash;
import com.mf.service.ServiceSmash;
import com.mf.entity.req.search.SearchReqSmash;

import java.util.List;

/**
 * 粉碎工单
 */
@RestController
@RequestMapping("smash")
@Tag(name = "粉碎工单")
public class ControllerSmash {
    @Resource
    private ServiceSmash servicesmash;

    @Resource
    private ServiceSmashDetail serviceSmashDetail;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    public Result<Page<DtoSmash>> findPage(@RequestBody SearchReqSmash searchReqSmash) {
        return Result.success(servicesmash.findPageByParams(searchReqSmash));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    public Result<List<DtoSmash>> list(@RequestBody SearchReqSmash searchReqSmash) {
        return Result.success(servicesmash.findListByParams(searchReqSmash));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqSmash reqSmash) {
        servicesmash.saveSmash(UtilConverter.convert(reqSmash, DtoSmash.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqSmash reqSmash) {
        // 暂时不能修改客户,当前明细没有做保存之类的验证修改后可能一个工单有多个客户数据
        reqSmash.setCustomerId(null);
        reqSmash.setCustomerName(null);
        servicesmash.updateById(UtilConverter.convert(reqSmash, DtoSmash.class));
        return Result.success();
    }

    @Operation(summary = "删除")
    @PostMapping("/delete/{smashId}/{id}")
    public Result<Void> delete(@PathVariable Integer smashId, @PathVariable("id") Integer id) {
        if (serviceSmashDetail.countBySmashId(smashId) > 0) {
            return Result.fail("该工单有数据,不能删除");
        }
        servicesmash.removeById(id);
        return Result.success();
    }

}
